﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Data;
using KTXD.Entities;

namespace UserKTXD.DCTEDU_.ThuVienAnh
{
    public partial class QLHinhAnh : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCatalog.BindBoSuuTap(cboBoSuuTap);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
            }
        }

        protected void Bind_Data(int PageNo)
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            if (cboBoSuuTap.SelectedValue != "0")
                WhereClause += " AND IdBoSuuTap=" + cboBoSuuTap.SelectedValue;
            if (Common.StandardString(txtTen.Text).Length > 0)
                WhereClause += " AND Ten LIKE '%" + Common.StandardString(txtTen.Text) + "'%";
            TList<HinhAnh> TList_Catalog = DataRepository.HinhAnhProvider.GetPaged(WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize, out TotalRows);
            if (TList_Catalog.Count == 0) dlHinhAnh.Caption = "Không tồn tại hình ảnh";
            dlHinhAnh.DataSource = TList_Catalog;
            dlHinhAnh.DataBind();

            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        public string GetHinhAnh(object IdBoSuuTap, object HinhAnh)
        {
            return Common.sPathUpBoSuuTap + "/" + IdBoSuuTap + "/Thumb/" + HinhAnh;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
                Pager1.CurrentIndex = currnetPageIndex;
                Bind_Data(currnetPageIndex);
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLHinhAnh.aspx.cs", "Pager1_Command()", ex.ToString());
            }
        }

        protected void cmdTimKiem_Click(object sender, ImageClickEventArgs e)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }

        protected void cmdTimLai_Click(object sender, ImageClickEventArgs e)
        {
            cboBoSuuTap.SelectedValue = "0";
            txtTen.Text = "";
        }

        protected void dlHinhAnh_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("Delete"))
            {
                int IdHinhAnh = Convert.ToInt32(e.CommandArgument.ToString());
                HinhAnh entHinhAnh = DataRepository.HinhAnhProvider.GetById(IdHinhAnh);
                if (entHinhAnh != null)
                {
                    string sFilePath = Server.MapPath(Common.sPathUpBoSuuTap);
                    string sThumbPath = sFilePath + "\\" + entHinhAnh.Id.ToString() + "\\Thumb\\" + entHinhAnh.Ten;
                    string sFullPath = sFilePath + "\\" + entHinhAnh.Id.ToString() + "\\Full\\" + entHinhAnh.Ten;
                    if (System.IO.File.Exists(sThumbPath))
                    {
                        System.IO.File.Delete(sThumbPath);
                    }
                    if (System.IO.File.Exists(sFullPath))
                    {
                        System.IO.File.Delete(sFullPath);
                    }
                    DataRepository.HinhAnhProvider.Delete(IdHinhAnh);
                    Bind_Data(Pager1.CurrentIndex);
                }
            }
        }
    }
}